// pages/extract/extract.js
import {
    getExtract,
    getId,
    getMoneyList
} from '../../api/user'
import {
    getworth,
    apply
} from '../../api/article.js'
import {
    formatTime
} from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        show2: false,
        amount: 0,
        txParams: {
            amount: 0,
            extractId: null,
            type: '2',
            bankAcc: null,
            bankName: null,
            accName: null,
        },
        params: {
            pageIndex: 1,
            pageSize: 100,
        },
        lastPage: false,
        list: [],
        shouyi: [],
        hasExtract: 0,
        availableMount: 0,
        totalMoney: 0,
        lockMoney: 0,
    },
    onChange(event) {
        console.log(event)
        const key = `txParams.${event.currentTarget.dataset.key}`
        this.setData({
            [key]: event.detail
        })
    },
    onChangeType(e) {
        this.setData({
            'txParams.type': e.detail
        })
    },

    handleOpen() {
        this.setData({
            show: true
        })
    },
    async handleTXSubmit() {
        if(this.data.txParams.type==2 && (!this.data.txParams.accName || !this.data.txParams.bankName || !this.data.txParams.bankAcc)){
            wx.showToast({
              title: '请填写银行信息',
              icon:'error'
            })
            return
        }
        const reslut = await getId({
            type: 8
        })
        this.setData({
            'txParams.extractId':reslut.content
        })
        console.log(this.data.txParams, 'xx')
        const res = await apply(this.data.txParams)
        console.log(res,'=====================')
        if(typeof res == 'string'){
            wx.showToast({
                title: res,
                icon: 'none'
            })
            return
        }
        if (!res.status) {
            wx.showToast({
                title: '提现成功',
                icon: 'success'
            })
            this.setData({
                show2: false,
                show: false,
            })
            this.onLoad()
            this.onShow()
        }
    },
    onClose() {
        this.setData({
            show2: false,
            show: false,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const res = await getworth({})
        console.log(res, '余额')
        this.setData({
            hasExtract: res.content ? res.content.hasExtract : '0.00',
            availableMount: res.content ? res.content.availableMount : '0.00',
            totalMoney: res.content ? res.content.totalMoney : '0.00',
            lockMoney: res.content ? res.content.lockMoney : '0.00',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        this.data.params.pageIndex = 1
        this.setData({
            list: [],
            lastPage: false
        })
        this.init()
    },
    async init() {
        const res = await getExtract(this.data.params)
        this.setData({
            list: this.data.list.concat(res.content),
            lastPage: res.content.lastPage,
        })
    },
    async handleOpenDetail() {
        const res = await getMoneyList(this.data.params)
        if (res.content && res.content.length) {
            res.content.forEach(el => {
                el.time = formatTime(el.createTime)
            })
        }
        this.setData({
            show2: true,
            shouyi: res.content
        })

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (!this.data.lastPage) {
            this.setData({
                ['params.pageIndex']: this.data.params.pageIndex += 1
            })
            this.init()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})